package com.amon.amonplayer.player.widget;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.amon.amonplayer.player.AmonPlayer;
import com.amon.amonplayer.player.MediaErrorListener;
import com.amon.amonplayer.player.MediaPreparedListener;
import com.amon.amonplayer.player.VideoInfoListener;

import java.io.FileDescriptor;

public class VideoView extends SurfaceView implements
        MediaPreparedListener, MediaErrorListener, VideoInfoListener {
    private final AmonPlayer mAmonPlayer;
    private final AssetManager mAssetManager;
    private Context mContext;

    private static Handler mHandler;

    private int mWidth = 0;
    private int mHeight = 0;
    private int mVideoWidth = 0;
    private int mVideoHeight = 0;

    public VideoView(Context context) {
        this(context, null);
    }

    public VideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mAssetManager = context.getAssets();
        mHandler = new Handler(context.getMainLooper());
        mAmonPlayer = new AmonPlayer(context);
        mAmonPlayer.setOnPreparedListener(this);
        mAmonPlayer.setOnErrorListener(this);
        mAmonPlayer.setOnVideoInfoListener(this);

        SurfaceHolder holder = getHolder();
        holder.setFormat(PixelFormat.RGBA_8888);
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {
                Log.i("TAG", "surfaceCreated");

            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
                Log.i("TAG", "surfaceChanged width:" + width + " height:" + height);
                mWidth = width;
                mHeight = height;
            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
                mAmonPlayer.stop();
            }
        });
    }

    public void play(String uri) {
        stop();
        mAmonPlayer.setDataSource(uri);
        mAmonPlayer.prepareAsync();
    }

    public void playFd(FileDescriptor fd) {
        try {
            stop();
            mAmonPlayer.setDataSource(fd);
            mAmonPlayer.prepareAsync();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void playFd(String uri) {
        try {
            stop();
            AssetFileDescriptor afd = mAssetManager.openFd(uri);
            mAmonPlayer.setDataSource(afd.getFileDescriptor());
            mAmonPlayer.prepareAsync();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void pause() {
        mAmonPlayer.pause();
    }

    public void stop() {
        mAmonPlayer.stop();
    }

    @Override
    public void onPrepare() {
        mHandler.post(() -> {
            mAmonPlayer.setSurface(getHolder().getSurface());
            mAmonPlayer.play();
        });
    }

    @Override
    public void onError(int code, String msg) {

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        Log.i("TAG", "onMeasure width:" + widthSize + " height:" + heightSize);
        mWidth = widthSize;
        mHeight = heightSize;
    }

    @Override
    public void onSizeInfo(int width, int height) {
        Log.i("TAG", "width:" + width + " height:" + height);
        mVideoWidth = width;
        mVideoHeight = height;
        mHandler.post(() -> {
            ViewGroup.LayoutParams params = getLayoutParams();
            params.width = mWidth;
            params.height = mWidth * mVideoHeight / mVideoWidth;
            requestLayout();
        });
    }
}
